import 'dart:convert';

import 'package:du_application/http/HttpUtil.dart';
import 'package:du_application/http/Options.dart';
import 'package:du_application/model/OrderBuyModel.dart';
import 'package:du_application/model/ResultModel.dart';
import 'package:flutter/material.dart';
import 'package:provide/provide.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'AuthProvider.dart';

class OrderProvider with ChangeNotifier {
   int size=1;
   String content;
  OrderBuyModel orderBuyModel;
    Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
    getOrder(context) async {
    await _prefs.then((ins) async{
    Options options =  Options(Provide.value<AuthProvider>(context).token);
   await HttpUtil.getInstance().get("/user/order",options:options.options).then((data) {
     orderBuyModel=OrderBuyModel.fromJson(json.decode(data));
     notifyListeners();
     });
});
  }
  replaceSize(int size){
    this.size=size;
   notifyListeners();
    }
   replaceContent(String content){
    this.content=content;
   notifyListeners();
    }

  comment(context,int id) async {
   return await _prefs.then((ins) async{
    Options options =  Options(Provide.value<AuthProvider>(context).token);
  return  await HttpUtil.getInstance().post("/order/comment",data: {"content":content,"size":size,"orderGoodsId":id,"hasPicture":false},options:options.options).then((data) async {
    
    if(ResultModel.fromJson(json.decode(data)).errno==0){
       await getOrder(context);
       return true;
    }else  return false;

     });
});
  }
  }
